package src.main.controller;

import jakarta.servlet.http.HttpServletRequest;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author huwei
 */
@RestController
@RequestMapping("/orc")
public class OrcController {

    @PostMapping("/english")
    public ResponseEntity<String> OrcEXnglish(HttpServletRequest request) throws IOException, TesseractException {
        BufferedImage image = ImageIO.read(request.getInputStream());
        ITesseract instance = new Tesseract();
        instance.setDatapath("src/main/resources/data");
        instance.setLanguage("eng");
        String content = instance.doOCR(image);
        return ResponseEntity.ok(content);
    }

    @PostMapping("/chinese")
    public ResponseEntity<String> OrcChinese(HttpServletRequest request, @RequestParam("image") MultipartFile file) throws TesseractException, IOException {
        BufferedImage image = ImageIO.read(file.getInputStream());
        ITesseract instance = new Tesseract();
        instance.setDatapath("src/main/resources/data");
        instance.setLanguage("chi_sim");
        String content = instance.doOCR(image);
        return ResponseEntity.ok(content);
    }


}
